/**
 * 垃圾扫描和清理任务执行句柄
 */

package com.tools.cleanmaster.controller;

import android.content.Context;

import com.tools.cleanmaster.model.TrashType;
import com.tools.cleanmaster.model.TrashesData;
import com.tools.cleanmaster.utils.FeatureConfig;
import com.tools.cleanmaster.utils.LogHelper;
import com.tools.cleanmaster.utils.TrashUtils;

import java.util.List;

/**
 * @author ouyang
 */
public abstract class TrashHandler {
    private static final String TAG = "TrashHandler";

    protected TrashType[] mTrashType;

    protected Context mContext;

    private String[] mRootDirs;

    /**
     * 当前实际进度
     */
    private int mCurRealProgress = 0;

    /**
     * 当前显示进度
     */
    private int mCurShowProgress = 0;

    /**
     * 连续0进度数
     */
    private int mZeroProgressNum = 0;

    private int mScannedTypeNum = 0;

    private long mStartTime;

    private int mZeroProgressNumMax;

    protected boolean mStopRequested = false;

    /**
     * 连续0进度累计最大值
     */
    private static final int ZERO_PROGRESS_NUM_MAX = 1000;

    public TrashHandler(Context context, TrashType[] trashType) {
        this.mContext = context;
        this.mTrashType = trashType;
    }

    protected abstract int getTrashTypesCount();

    public abstract void scan(ITrashScanListener listener);

    public abstract TrashesData getTrashesData();

    protected abstract List<ITrashScanListener> getScanListeners();

    protected String[] getRootDirs() {
        if (mRootDirs == null) {
            mRootDirs = TrashUtils.findSDPaths(mContext);
        }
        return mRootDirs;
    }

    /**
     * @param increaseProgress 新增进度
     * @param progressDesc     进度描述
     */
    protected void updateProgress(int increaseProgress, String progressDesc) {
        // 是否已经停止，如果停止则不执行
        if (mStopRequested) {
            return;
        }
        mCurRealProgress += increaseProgress;
        if (mCurRealProgress > 100) {
            mCurRealProgress = 100;
            if (FeatureConfig.DEBUG_LOG) {
                LogHelper.e(TAG, "mCurProgress > 100");
            }
        }
        if (increaseProgress == 0) {
            mZeroProgressNum++;
        } else {
            mZeroProgressNum = 0;
        }
        // 计算mZeroProgressNumMax
        if (mCurShowProgress < 70) {
            mZeroProgressNumMax = ZERO_PROGRESS_NUM_MAX;
        } else if (mCurShowProgress < 85) {
            mZeroProgressNumMax = ZERO_PROGRESS_NUM_MAX * 2;
        }
        // 当累计增加的进度为0的数大于限度(mZeroProgressNumMax次)时，先增加进度1
        if (mZeroProgressNum > mZeroProgressNumMax && mCurShowProgress < 92) {
            mZeroProgressNum = 0;
            mCurShowProgress++;
        } else {
            if (mCurShowProgress < mCurRealProgress) {
                mCurShowProgress = mCurRealProgress;
            }
        }
        if (mCurShowProgress > 100) {
            mCurShowProgress = 100;
            if (FeatureConfig.DEBUG_LOG) {
                LogHelper.e(TAG, "mCurShowProgress > 100");
            }
        }
        if (getScanListeners() != null) {
            // 触发listener
            for (ITrashScanListener listener : getScanListeners()) {
                if (listener != null) {
                    listener.onScanProgressUpdate(progressDesc, mCurShowProgress, this
                            .getTrashesData().getSize());
                }
            }
        }
    }

    /**
     * 重置Proress信息
     */
    private void resetProress() {
        mCurRealProgress = 0;
        mCurShowProgress = 0;
        mZeroProgressNum = 0;
        mScannedTypeNum = 0;
        mStopRequested = false;
    }

    protected void onScanStart() {
        if (getScanListeners() != null) {
            // 触发listener
            for (ITrashScanListener listener : getScanListeners()) {
                if (listener != null) {
                    listener.onScanStart();
                }
            }
        }
        if (FeatureConfig.DEBUG_LOG) {
            mStartTime = System.currentTimeMillis();
            LogHelper.d(TAG, "onTrashScanStart mStartTime=" + mStartTime);
        }
        resetProress();
    }

    /**
     * @param type 完成的类型
     * @param size 完成类型的大小
     */
    protected void onTrashTypeFinish(TrashType type, long size) {
        if (FeatureConfig.DEBUG_LOG) {
            LogHelper.i(TAG, "onTrashTypeFinish type=" + type + ",size=" + size
                    + ",mScanTypeTotal=" + this.getTrashTypesCount()
                    + ",mScannedTypeNum=" + mScannedTypeNum + ",stopRequested=" + mStopRequested);
        }
        // 是否已经停止，如果停止则不执行
        if (mStopRequested) {
            return;
        }
        mScannedTypeNum++;
        if (getScanListeners() != null) {
            // 触发listener
            for (ITrashScanListener listener : getScanListeners()) {
                if (listener != null) {
                    listener.onScanTypeFinish(type, size);
                }
            }
            if (mScannedTypeNum == this.getTrashTypesCount()) {
                // 触发onFinishScan
                onScanFinish();
            }
        }
    }

    protected void onScanFinish() {
        if (FeatureConfig.DEBUG_LOG) {
            LogHelper.d(TAG, "onTrashTypeFinish onFinishScan mStartTime=" + mStartTime
                    + ",consume time =" + (System.currentTimeMillis() - mStartTime));
        }
        if (getScanListeners() != null) {
            // 触发listener
            for (ITrashScanListener listener : getScanListeners()) {
                if (listener != null) {
                    listener.onScanFinish();
                }
            }
            getScanListeners().clear();
        }
    }

    protected void onScanStop() {
        if (FeatureConfig.DEBUG_LOG) {
            LogHelper.d(TAG, "onStopScan");
        }
        mStopRequested = true;
        if (getScanListeners() != null) {
            // 触发listener
            for (ITrashScanListener listener : getScanListeners()) {
                if (listener != null) {
                    listener.onScanStop();
                }
            }
            getScanListeners().clear();
        }
    }

}
